/**
 * @author: welling
 * okey007@163.com	19880812
 */
Weifox.apps.netease = {
	name:'netease',
	domain:'http://api.163.com/',
	icon:'',
	appKey:'BomdSZP4Shl1WLsA',
	appSecret:'kUWzs4Hdo8u6tG0s5QLToU5wpEefZNza',
	/***
	 * 发微博
	 * @param {Object} user
	 * @param {Object} data
	 */
	send:function(user,data){
		var req={};
		return;
		switch(data.type){
			case 'nl':
				req.url='http://api.netease.com/statuses/update.json';
				req.args=[['status', data.text]];
				break;
			case 're-cm':
			case 'cm':
				req.url='http://api.netease.com/statuses/update.json';
				data.text=unescape(encodeURIComponent('@'+data.sender))+' '+data.text;
				req.args=[['status', data.text],['in_reply_to_status_id',data.id]];
				break;
			case 'rp':
				req.url='http://api.netease.com/statuses/update.json';
				data.text=data.text+' '+unescape(encodeURIComponent('转:@'+data.sender+' '+data.content));
				req.args=[['status', data.text],['repost_status_id',data.id]];
				break;
			case 're-dm':
				req.url='http://api.netease.com/direct_messages/new.json';
				req.args=[['text', data.text],['user',data.id]];
				req.args.push(['in_reply_to_id',data.in_reply_to_id ]);
				break;
			case 'nl-pic':
				req.url='http://api.netease.com/photos/upload.json';
				req.args=[['status', data.text]];
				req.stream={
					photo:data.pic
				}
				break;
			default:
				return;
		}
//		Weifox.debug.showObj(data);
		this.sendRequest(user,req);
	},
	/**
	 * 
	 * @param {Object} method 
	 * @param {Object} para
	 * @param {Object} callback	返回一个User Object
	 * user_name user_id user_icon para1 para2 method enable 
	 * timeline{
	 * 	friends.. xx.. xx..
	 * } 
	 */
	parseText:function(node,text){
		node.appendChild(document.createTextNode(text));
	},
	handleError:function(err){
		//Weifox.debug.showObj(err);
		alert('Error netease:'+new String(err.content));
	},
	/**
	 * 
	 * @param {Object} method
	 * @param {Object} para
	 * @param {Object} callback
	 */
	verify:function(method,para,callback){
		if(method=='oauth'){////oauth
			var netease=this;
			var data={
				url:'http://api.t.163.com/oauth/access_token',
				success:function(text){//获取到两个参数后,还要发一条获取用户信息...双重回调,TODO:优化
					var a=text.split('&');
					var token=a[0].split('=')[1];
					var token_secret=a[1].split('=')[1];
					var user={
						app:'netease',
						method:'oauth',
						para1:token,
						para2:token_secret
					}
					//
					//获取用户的具体信息
					netease.sendRequest(user,{
						url:'http://api.t.163.com/account/verify_credentials.json',
						success:function(u){
							callback({
								app:'netease',
								method:'oauth',
								para1:token,
								para2:token_secret,
								user_id:u.id,
								user_name:u.name,
								user_icon:u.profile_image_url
							});
						},
						error:callback
					});
				},
				error:callback
			}
			var net=Weifox.net;
			net.makeOAuth(data,this.appKey,this.appSecret,{
				oauth_verifier:para.pn,
				oauth_token:para.token,
				oauth_token_secret:para.token_secret
			},false);
			new net.Request().send(data);
		}
	},
	getOAuthCode:function(callback){
		var data={
			method:'GET',
			url:'http://api.t.163.com/oauth/request_token',
			success:function(text){
				
				var x=text.split('&');
				var token=x[0].split('=')[1];
				var token_secret=x[1].split('=')[1];;
				Weifox.browser.openLink('http://api.t.163.com/oauth/authorize?oauth_token='
					+token,true);
				callback({
					token:token,
					token_secret:token_secret
				});
			},
			error:callback
		};
		var net=Weifox.net;
		net.makeOAuth(data,this.appKey,this.appSecret,{oauth_callback:''},false);
		new net.Request().send(data);
	},
	/**
	 * 
	 * 返回一个封好数据的Request,直接Send即可,
	 * Data需先设置好再传入,否则OAuth的签名计算会出错.
	 * 适用于已授权的API,
	 * @param {Object} user
	 * @param {Object} data
	 */
	sendRequest:function(user,data){
		var method=user.method.toUpperCase();
		if(!data.args)data.args=[];
		data.method="GET";
		if(method=='OAUTH'){
			Weifox.net.makeOAuth(data,this.appKey,this.appSecret,
					{oauth_token:user.para1,oauth_token_secret:user.para2},true);
		}else{
			throw new Error('This app does not support '+method);
		}
		var req=new Weifox.net.Request();
		data.error=this.handleError;
		req.send(data);
	}



}